import android
import time
from sms_tools import SMSTools
from contacts_tools import ContactTools

droid = android.Android()


        


class AndroidWindow(android.Android):
    def __init__(self):
        self.layout = None
        self.current_process = 'start_main'
        
        super(AndroidWindow, self).__init__()
        
        
        
        current_layout = self.build_layout('main')
        self.run(current_layout)
            
                
    def event_loop(self):
        while True:
            event = self.eventWait().result
            #print event
            if event['name'] == 'click':
                event_id = event['data']['id']
                print event_id
                
            elif event['name'] == 'screen':
                if event['data'] == 'destroy':
                    return
        
    def run(self, layout):
        '''
        This will be the method that executes the entirety
        of the program.
        
        Should run the window manager
        '''
        
        self.fullShow(layout)
        self.event_loop()
        self.fullDismiss()
    
    def build_layout(self, layout_type):
        '''
        This method will build the layout
        
        Takes one perameter, which will be the layout type.
        The type will determine which xml file it loads for the layout.
        '''
        
        if layout_type in ['main', 'input']:
            if layout_type == 'main':
                layout_file = '/mnt/sdcard/sl4a/scripts/EncryptSMS/layouts/main.xml'
                
                
            elif layout_type == 'input':
                layout_file = '/mnt/sdcard/sl4a/scripts/EncryptSMS/layouts/input.xml'
            else:
                layout_file = '/mnt/sdcard/sl4a/scripts/EncryptSMS/layouts/main.xml'
            
            f = open(layout_file, 'r')
            return f.read()
        
        else:
            '''
            TODO: need default action or error action
            '''
            print 'Error!!!'
            return TypeError, 'Layout Type not valid'
            exit()
                
    
    def process_input(self):
        '''
        This method will grab user input and encrypt it. Then return the encrypted value to be texted
        
        TODO: This method should be in a different class. 12.14.12 JB
        '''
        pass
    
def main():
    items = ['Encrypt SMS', 'Set Favorites', 'Non Encrypt SMS', 'App Settings']
    options = {
        'Encrypt SMS': AndroidWindow,
        'Get Contacts':ContactTools().print_contacts,
        'SMS Messages':SMSTools()._print_sorted_messages
    }
    droid.dialogCreateAlert('You must choose, but choose wisely!')
    droid.dialogSetItems(items)
    droid.dialogSetNegativeButtonText('Cancel')
    droid.dialogShow()
    result = droid.dialogGetResponse().result
    choice = items[result['item']]
    options[choice]()
    
    
    
    
if __name__ == '__main__':
    main()
    #sms = SMSTools()
    #sms.print_messages()
    
    #cms = ContactTools()
    #cms.print_contacts()
    
    
    
    
    